<?php
    $config = fopen("config.ini", "r");
    if ($config == false){
        header("Location: configurar.html");
    }
    else{
    require 'conectarBD.php';
    require 'verificarLogin.php';
?>
<!DOCTYPE html>
<html lang="es">
    <head>
        <title>Módulo de gestión de contratos.</title>
        <link rel="STYLESHEET" type="text/css" href="estilos/general.css">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="javascript/funciones.js"></script>
    </head>
    <body>
        <nav>
            <?php
            include 'menuSuperior.php';
            ?>
        </nav>  
        <section>
            <?php
                if(isset($_POST["contrato"])){
                    $contrato = $_POST["contrato"];          
                }
                else{
                    $query = "select idContrato from Contrato co order by idContrato desc";
                    $res = mysqli_query($con, $query);
                    $row = mysqli_fetch_array($res);
                    $contrato = $row["idContrato"];
                }
                    if($_POST["atras"] and $contrato > 1)
                        $contrato--;
                    else if($_POST["adelante"]){
                        $query = "select idContrato from Contrato co order by idContrato desc";
                        $res = mysqli_query($con, $query);
                        $row = mysqli_fetch_array($res);
                        if($contrato < $row["idContrato"])
                            $contrato++;
                    }else if($_POST["actualizar"]){
                        $query = "select idCliente from Contrato where idContrato = " . $contrato;
                        $res = mysqli_query($con, $query);
                        $row = mysqli_fetch_array($res);
                        $query = "update Cliente set C_Nombre = '" . $_POST["nombre"] . "', C_Direccion = '" . $_POST["direccion"] . "', C_Identificacion = '" . $_POST["identificacion"] . "', C_Telefono = '" . $_POST["telefono"] .  "' where idCliente = " . $row["idCliente"];
                        $res = mysqli_query($con, $query);
                        
                        $query = "update Contrato set idTipoAparato = " . $_POST["aparato"];
                        $query = $query . ", idMarca = " . $_POST["marca"];
                        $query = $query . ", idTecnico = " . $_POST["tecnico"];
                        $query = $query . ", C_Modelo = '" . $_POST["modelo"] . "'";
                        $query = $query . ", C_Serie = '" . $_POST["serie"] . "'";
                        $query = $query . ", C_Descripcion = '" . $_POST["descripcion"] . "'";
                        $query = $query . " where idContrato = " . $_POST["contrato"];
                        $res = mysqli_query($con, $query);
                        
                        $query = "select idEstado, EC_Presupuesto from EstadoContrato where idContrato = " . $_POST["contrato"];
                        $query = $query . " order by EC_Fecha desc";
                        $res = mysqli_query($con, $query);
                        $row = mysqli_fetch_array($res);
                        if ($row["idEstado"] == $_POST["estado"])
                            if($row["EC_Valor"] != $_POST["presupuesto"]){
                                $query = "insert into EstadoContrato (idEstado, idContrato, EC_Fecha, EC_Valor) values";
                                $query = "(" . $_POST["estado"] .", " . $_POST["contrato"] .", '" . date() ."', " . $_POST["presupuesto"] .")";
                                echo $query;
                            }
                                
                    }
                    $query = "select * from Cliente c, Contrato co, TipoAparato ta, Marca m";
                    $query = $query . " where c.idCliente = co.idCliente";
                    $query = $query . " and ta.idTipoAparato = co.idTipoAparato";
                    $query = $query . " and m.idMarca = co.idMarca";
                    $query = $query . " and co.idContrato = " . $contrato;
                    
                    $res = mysqli_query($con, $query);
                    $row = mysqli_fetch_array($res);
                    
                    
                    echo "<form method='POST'>\n";
                                    
                    echo "<label for='contrato'>No. contrato</label>\n";
                    echo "<input type='number'name='contrato' readonly size=5 id='contrato' value='" . $row["idContrato"] . "'>\n";
                    
                    echo "<fieldset>\n";
                    echo "<legend>Datos de cliente</legend>\n";
                    
                    echo "<span class='dato'>";  
                    echo "<label for='nombre'>Nombre</label>\n";
                    echo "<input type='text'name='nombre' id='nombre' value='" . $row["C_Nombre"] . "'>\n";
                    echo "</span>";
                    
                    echo "<span class='dato'>";  
                    echo "<label for='direccion'>Dirección</label>\n";
                    echo "<input type='text'name='direccion' size=35 id='direccion' value='" . $row["C_Direccion"] . "'>\n";
                    echo "</span>";
                    
                    echo "<span class='dato'>";  
                    echo "<label for='identificacion'>Identificación</label>\n";
                    echo "<input type='text'name='identificacion' id='identificacion' value='" . $row["C_Identificacion"] . "'>\n";
                    echo "</span>";
                    
                    echo "<span class='dato'>";  
                    echo "<label for='telefono'>Teléfono(s)</label>\n";
                    echo "<input type='tel'name='telefono' id='telefono' value='" . $row["C_Telefono"] . "'>\n";
                    echo "</span>";

                    echo "</fieldset>\n";
                    
                    echo "<fieldset>\n";
                    echo "<legend>Datos del aparato</legend>\n";
                    
                    $query = "select * from TipoAparato";
                    $res = mysqli_query($con, $query);
                    
                    echo "<span class='dato'>";  
                    echo "<label for='aparato'>Tipo de aparato</label>\n";

                    echo "<select id='aparato' name='aparato'>\n";

                    while($row2 = mysqli_fetch_array($res)){
                        if ($row2["idTipoAparato"] == $row["idTipoAparato"])
                            echo "<option selected value='" . $row2["idTipoAparato"] ."'>" . $row2["TA_Nombre"] . "</option>\n";
                        else
                            echo "<option value='" . $row2["idTipoAparato"] ."'>" . $row2["TA_Nombre"] . "</option>\n";
                    }

                    echo "</select>\n";
                    echo "</span>";
                    
                    $query = "select * from Marca";
                    $res = mysqli_query($con, $query);
                    
                    echo "<span class='dato'>";  
                    echo "<label for='marca'>Marca</label>\n";

                    echo "<select id='marca' name='marca'>\n";

                    while($row2 = mysqli_fetch_array($res)){
                        if ($row2["idMarca"] == $row["idMarca"])
                            echo "<option selected value='" . $row2["idMarca"] ."'>" . $row2["M_Marca"] . "</option>\n";
                        else
                            echo "<option value='" . $row2["idMarca"] ."'>" . $row2["M_Marca"] . "</option>\n";
                    }

                    echo "</select>\n";
                    echo "</span>";
                    
                    $query = "select * from Tecnico";
                    $res = mysqli_query($con, $query);
                    
                    echo "<span class='dato'>";  
                    echo "<label for='tecnico'>Técnico</label>\n";

                    echo "<select id='tecnico' name='tecnico'>\n";
                    
                    while($row2 = mysqli_fetch_array($res)){
                        if($row2["idTecnico"] == $row["idTecnico"])
                            echo "<option selected value='" . $row2["idTecnico"] ."'>" . $row2["Te_Nombre"] . "</option>\n";
                        else
                            echo "<option value='" . $row2["idTecnico"] ."'>" . $row2["Te_Nombre"] . "</option>\n";
                    }
                    
                    echo "</select>\n";
                    echo "</span>";
                    
                    echo "<span class='dato'>";  
                    echo "<label for='modelo'>Modelo</label>\n";
                    echo "<input type='text' name='modelo' id='modelo' value='" . $row["C_Modelo"] . "'>\n";
                    echo "</span>";
                    
                    echo "<span class='dato'>";  
                    echo "<label for='serie'>Serie</label>\n";
                    echo "<input type='text' name='serie' id='serie' value='" . $row["C_Serie"] . "'>\n";
                    echo "</span>";
                    
                    echo "<span class='dato'>";  
                    echo "<label for='fechaIngreso'>Fecha de ingreso</label>\n";
                    echo "<input type='date' name='fechaIngreso' id='fechaIngreso' disabled value=" . $row["C_Fecha"] . ">\n";
                    echo "</span>";
                    
                    echo "<textarea maxlength=255 name='descripcion' id='descripcion'>" . $row["C_Descripcion"] . "</textarea>\n";
                    
                    
                    $query = "select * from Estado";
                    $res = mysqli_query($con, $query);
                    
                    echo "<span class='dato'>";  
                    echo "<label for='estado'>Estado</label>\n";
                    
                    echo "<select id='estado' name='estado'>\n";
                    
                    echo $row["idEstado"];
                    
                    while($row2 = mysqli_fetch_array($res)){
                        //if($row2["idEstado"] == $row["idEstado"])
                        //    echo "<option selected value='" . $row2["idEstado"] ."'>" . $row2["E_Nombre"] . "</option>\n";
                        //else
                            echo "<option value='" . $row2["idEstado"] ."'>" . $row2["E_Nombre"] . "</option>\n";
                    }
                    
                    echo "</select>\n";
                    echo "</span>";
                    
                    echo "<span class='dato'>";  
                    echo "<label for='presupuesto'>Valor presupuestado</label>\n";
                    echo "<input type='number' step=50 name='presupuesto' id='presupuesto' min=0>\n";
                    echo "</span>"; 
                    
                    echo "<span class='dato'>";  
                    echo "<label for='ordenado'>Valor ordenado</label>\n";
                    echo "<input type='number' step=50 name='ordenado' id='ordenado' min=0>\n";
                    echo "</span>"; 
                    
                    if($row["E_Nombre"] == "Ordenado"){
                        echo "<span class='dato'>";  
                        echo "<label for='fechaOrdenado'>Fecha de ordenado</label>\n";
                        echo "<input type='date' name='fechaOrdenado' id='fechaOrdenado' disabled value=" . $row["E_FechaEstado"] . ">\n";
                        echo "</span>";
                    }
                    else if ($row["E_Nombre"] == "Entregado"){
                        echo "<span class='dato'>";  
                        echo "<label for='fechaEntregado'>Fecha de entrega</label>\n";
                        echo "<input type='date' name='fechaEntregado' id='fechaEntregado' disabled value=" . $row["E_FechaEstado"] . ">\n";
                        echo "</span>";
                    }
                    
                    echo "</fieldset>\n";
                    echo "<input type='submit' value='<<' id='atras' name='atras'>";
                    echo "<input type='submit' value='Actualizar' id='actualizar' name='actualizar'>";
                    echo "<input type='submit' value='>>' id='adelante' name='adelante'>";
                    echo "</form>\n";
                }
            ?>
        </section>
        <footer>Creado por: Luis Felipe Gonzalez Moreno, 2013.</footer>
    </body>
</html>
<?php
?>
